package com.biqee.action.crm;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.biqee.action.base.BaseAction;
import com.biqee.exception.BusinessUnCheckedException;
import com.biqee.service.crm.CustomerService;
import com.biqee.util.CsvUtils;

public class ImportOrderAction extends BaseAction {
	//tid,buyer_nick,buyer_alipay_no,买家应付货款,post_fee,point_fee,total_fee,buyer_obtain_point_fee,payment,real_point_fee,cod_status,buyer_message,receiver_name,receiver_address,运送方式,receiver_phone,receiver_mobile,created,pay_time,title,宝贝种类,物流单号,物流公司,buyer_memo,宝贝总数量,店铺Id,seller_nick,订单关闭原因,seller_cod_fee,buyer_cod_fee,invoice_name,trade_from
	private static final String CSV_HEAD_LINE= "tid,buyerNick,buyerAlipayNo,买家应付货款,postFee,pointFee,totalFee,buyerObtainPointFee,payment,realPointFee," +
												"codStatus,buyerMessage,receiverName,receiverAddress,运送方式,receiverPhone,receiverMobile,created,payTime,title," +
												"宝贝种类,物流单号,物流公司,buyerMemo,宝贝总数量,店铺Id,sellerNick,订单关闭原因,sellerCodFee,buyerCodFee,invoiceName," +
												"tradeFrom";
	@Resource
	private CustomerService customerService ;
	
	private  static String[] head= null;
	static{
		head = CSV_HEAD_LINE.split(",");
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private File uploadFile ;
	
	private String uploadFileContentType; 
	
	
	private String  uploadFileFileName; 

	
	public String  execute() {
		InputStream  in = null ;
		try {
		if(uploadFile!=null&&isCsvFile(uploadFileFileName)){
			  in = new FileInputStream(uploadFile);
			List<Map<String,String>> dataList = CsvUtils.parseCsv(in, head);
			customerService.importDataMap(-1L, dataList);
			return GLOBLE_RESULT_SUCCESS;
		}
		} catch (IOException e) {
			throw new BusinessUnCheckedException("上传文件失败");
		}finally{
			if(in !=null){
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return GLOBLE_RESULT_ERROR;
	}
	
	
	private boolean isCsvFile(String uploadFileFileName2) {
		
		if(uploadFileFileName2.endsWith("csv")){
			return true ;
			
		}
		throw new BusinessUnCheckedException("必须上传csv文件");
	}


	public String  test() {

		return "test";
	}
	
	public Object getModel() {
		// TODO Auto-generated method stub
		return null;
	}




	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}


	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}


	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}


	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	
	

}
